package com.seata.demo.cloud.datasource;

import java.util.ArrayList;
import java.util.List;

public class DynamicDataSourceHolder {
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
    private static final List<String> dataSourceIds = new ArrayList<>();
    public static final String DEFAULT_DATA_SOURCE = "DEFAULT_DATA_SOURCE";
    public static void setDataSourceType(String dataSourceType){
        contextHolder.set(dataSourceType);
    }

    /**
     * 如果获取的DataSourceType为空时
     * 默认走defaultDataSource
     * @return
     */
    public static String getDataSourceType(){
        String type = contextHolder.get();
        if(DEFAULT_DATA_SOURCE.equals(type)){
            return null;
        }else{
            return contextHolder.get();
        }
    }

    public static void clearDataSourceType(){
        contextHolder.remove();
    }
    public static boolean containsDataSource(String dataSourceId){
        return dataSourceIds.contains(dataSourceId);
    }

    public static void addDataSourceId(String dataSourceId){
        dataSourceIds.add(dataSourceId);
    }
}
